package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.warning.domain.entity.WirelessSensors;
import com.ruoyi.warning.domain.entity.WirelessSensorsManual;
import com.ruoyi.warning.domain.query.WirelessSensorQuery;
import com.ruoyi.warning.domain.vo.WirelessSensorVO;
import com.ruoyi.warning.mapper.WirelessSensorsMapper;
import com.ruoyi.warning.service.IDevicesService;
import com.ruoyi.warning.service.IWirelessSensorsManualService;
import com.ruoyi.warning.service.IWirelessSensorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-06-13
 */
@RestController
@RequestMapping("/wirelessSensors")
@Api(tags = "无线传感器", description = "用于处理无线传感器相关操作的控制器")
public class WirelessSensorsController {
    @Resource
    private IWirelessSensorsService wirelessSensorsService;
    @Resource
    private WirelessSensorsMapper wirelessSensorsMapper;
    @Resource
    private IWirelessSensorsManualService wirelessSensorsManualService;
    @Resource
    private IDevicesService devicesService;

    @ApiOperation("获取AC无线传感器列表")
    @GetMapping(value = "/getACPage")
    public R getACPage(WirelessSensorQuery query){
        Page<WirelessSensors> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<WirelessSensorVO> list = wirelessSensorsMapper.getAC(query);
        return R.ok(new TableDataInfo(list, page.getTotal()));
    }

    @ApiOperation("获取ac详细数据")
    @GetMapping(value = "/getDetail")
    public R getDetail(WirelessSensorQuery query){
        WirelessSensors wirelessSensors = wirelessSensorsService.getOne(new LambdaQueryWrapper<WirelessSensors>()
                .eq(WirelessSensors::getDeviceId,query.getDeviceId())
                .eq(WirelessSensors::getSensorType,"ciscowlc")
                .last("limit 1"));
        if(ObjectUtils.isEmpty(wirelessSensors)){
            wirelessSensors = wirelessSensorsManualService.getOne(new LambdaQueryWrapper<WirelessSensorsManual>()
                    .eq(WirelessSensorsManual::getDeviceId,query.getDeviceId())
                    .eq(WirelessSensors::getSensorType,"ciscowlc")
                    .last("limit 1"));
        }
        WirelessSensorVO wirelessSensorsVO = wirelessSensorsMapper.getAPOnline(query.getDeviceId());

        BeanUtils.copyProperties(wirelessSensors,wirelessSensorsVO);
        wirelessSensorsVO.setDevice(devicesService.getById(wirelessSensors.getDeviceId()));
        return R.ok(wirelessSensorsVO);
    }

    @ApiOperation("获取ap无线传感器列表")
    @GetMapping(value = "/getApPage")
    public R getAPPage(WirelessSensorQuery query){
        Page<WirelessSensors> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<WirelessSensors> list = wirelessSensorsService.list(new QueryWrapper<WirelessSensors>().lambda()
                .eq(WirelessSensors::getSensorClass,"clients")
                .eq(WirelessSensors::getDeviceId,query.getDeviceId()));
        if(ObjectUtils.isNotEmpty(list)){
            return R.ok(new TableDataInfo(list, page.getTotal()));
        }else {
            return R.ok(new TableDataInfo(wirelessSensorsManualService.list(
                    new QueryWrapper<WirelessSensorsManual>().lambda()
                    .eq(WirelessSensorsManual::getSensorClass,"clients")
                    .eq(WirelessSensorsManual::getDeviceId,query.getDeviceId())), page.getTotal()));
        }
    }

    @ApiOperation("手动添加无线设备的详细ACAP信息")
    @GetMapping(value = "/addACAP/{id}")
    public R addACAP(@PathVariable Integer id) {
        wirelessSensorsService.addACAP(id);
        return R.ok();
    }

    @ApiOperation("MAP手动添加无线设备的详细ACAP信息")
    @GetMapping(value = "/addACAPMap/{id}")
    public R addACAPMap(@PathVariable Integer id) {
        wirelessSensorsService.addACAPMap(id);
        return R.ok();
    }

}
